﻿
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>

<%--<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>--%>
<html>
<%--<head>--%>
<%--<script type="text/javascript" src="<%=request.getContextPath() %>/ckeditor/ckeditor.js"> </script>--%>
<%--</head>--%>
<body>
<form name="dataFrm" id="dataFrm" action="">
    <table width="100%" border="0" cellspacing="5" cellpadding="0">
        <thead>
        <tr>
            <td align="center" colspan="2" class="text_tabledetail2">增加新闻</td>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td style="text-align:right;" class="text_tabledetail2">分类</td>
            <td style="text-align:left;">
                <!-- 列出所有的新闻分类 -->
                <select name="categoryId">
                    <option value="1">国内</option>
                    <option value="2">国际</option>
                    <option value="3">娱乐</option>
                    <option value="4">军事</option>
                    <option value="5">财经</option>
                    <option value="6">天气</option>
                </select>
            </td>
        </tr>

        <tr>

            <td style="text-align:right;" class="text_tabledetail2"></td>
            </td>
        </tr>
        <tr>
            <td style="text-align:right;" class="text_tabledetail2">标题</td>
            <td style="text-align:left;"><input type="text" name="title" id="title" value=""/></td>
        </tr>
        <tr>
            <td style="text-align:right;" class="text_tabledetail2">作者</td>
            <td style="text-align:left;"><input type="text" name="author" id="author" value=""/></td>
        </tr>
        <tr>
            <td style="text-align:right;" class="text_tabledetail2">摘要</td>
            <td style="text-align:left;"><textarea id="summary" name="summary" rows="8" cols="50">
				</textarea></td>
        </tr>
        <tr>
            <td style="text-align:right;" class="text_tabledetail2">内容</td>
            <td style="text-align:left;">
                <div id="xToolbar"></div>
                <textarea id="content" name="content" rows="8" cols="30"></textarea></td>
        </tr>
        <tr>
            <td style="text-align:center;" colspan="2">
                <button type="button" name="return" onclick="add()">保存</button>
                <button type="button" class="page-btn" name="return"
                        onclick="javascript:location.href='newsDetailList.jsp'">返回
                </button>
            </td>
        </tr>
        </tbody>
    </table>
</form>
</body>
<script type="text/javascript" src="../js/jquery-1.9.1.js"></script>
<script type="text/javascript">
    var id = '${param.id}';
    $(function () {
        $.ajax({
            "url": "/newsServlet",
            "type": "post",
            "data": {"action": "getNewsById", "id": id},
            "dataType": "json",
            "success": callback,
            "error": function () {
                alert("请求异常，请与管理员联系!");
            }
        })
        function callback(data) {
            $("#title").val(data.title);
            $("#author").val(data.author);
            $("#summary").val(data.summary);
            $("#content").val(data.content);
            if (data.categoryId == 1) {
                $("[value='1']").prop("selected", true);
            } else if (data.categoryId == 2) {
                $("[value='2']").prop("selected", true);
            } else if (data.categoryId == 3) {
                $("[value='3']").prop("selected", true);
            } else if (data.categoryId == 4) {
                $("[value='4']").prop("selected", true);
            } else if (data.categoryId == 5) {
                $("[value='5']").prop("selected", true);
            } else if (data.categoryId == 6) {
                $("[value='6']").prop("selected", true);
            }
        }
    })
    function add() {
        // var pramsArray=$("#dataFrm").serializeArray();  //将表单编码json数组
        // var queryString=$.param(pramsArray);  //将json数组序列化为字符串
        var queryString = $("#dataFrm").serialize();
        $.ajax({
            "url": "/newsServlet?action=update&id="+id,
            "type": "post",
            "data": queryString,
            "dataType": "json",
            "success": addcallBack,
            "error": function () {
                    alert("请求异常,请与管理员联系")
                }
            })
    function addcallBack(data) {
        if (data.flag == "true") {
            location.href = "newsDetailList.jsp";
            alert("操作成功")
        } else {
            alert("操作失败")
            }
        }
    }


</script>
</html>